﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Utils;
using Cashier_Service;
using Cashier_Model;
using Cashier.utils;

namespace Cashier.payments
{
    public partial class PaymentsSortForm : Form
    {
        private MainForm mainform;
        private PaymentsControl paymentsControl;
        private PaymentsSortService paymentsSortService;
        private long id;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="paymentsControl"></param>
        public PaymentsSortForm(MainForm mainform, PaymentsControl paymentsControl)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.paymentsControl = paymentsControl;
            this.paymentsSortService = new PaymentsSortService();

            this.ControlsSetRights();
            this.CreateDataGridViewColumns();
            this.DataGridViewBindingData();
        }

        #region 功能方法实现

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            ContextMenuStrip[] menuStrips = { contextMenuStrip1 };
            RightsforControls.RightsforContextMenuStrip(mainform.Admin.RolePurviewtable, menuStrips);
        }

        /// <summary>
        /// 创建DataGridView列
        /// </summary>
        private void CreateDataGridViewColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("name", "名称", true, 70, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("type", "类型", true, 30, ActKey.CENTER, ActKey.CENTER, true, false));
        }

        /// <summary>
        /// DataGridView绑定数据
        /// </summary>
        private void DataGridViewBindingData()
        {
            String enterpriseNo = mainform.Admin.EnterpriseNo;
            DataTable dt = paymentsSortService.GetPaymentsSortTable(0, 0, enterpriseNo, 0);
            this.dataGridView1.DataSource = dt;
            if (dt != null && dt.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = true;
            }
            dataGridView1.Columns[0].Visible = false;
        }

        /// <summary>
        /// 保存收支分类
        /// </summary>
        private void SavePaymentsSort()
        {
            String name = textbox_name.Text.Trim();
            if (String.IsNullOrEmpty(name))
            {
                MessageBox.Show("请输入名称！");
                return;
            }
            int type = 0;
            if(radioButton_in.Checked)
                type = 1;
            else if(radioButton_out.Checked)
                type = 2;

            bool issuccess = false;
            PaymentsSort paymentsSort = new PaymentsSort();
            paymentsSort.Name = name;
            paymentsSort.Type = type;
            paymentsSort.EnterpriseNo = mainform.Admin.EnterpriseNo;
            if (id > 0)
            {
                paymentsSort.Id = id;
                int flag = paymentsSortService.UpdatePaymentsSort(paymentsSort);
                if (flag > 0)
                    issuccess = true;
            }
            else
            {
                long temp_id = paymentsSortService.InsertPaymentsSort(paymentsSort);
                if (temp_id > 0)
                    issuccess = true;
            }

            if (issuccess)
            {
                MessageBox.Show("保存成功！");
                this.ClearForm();
                this.DataGridViewBindingData();
                paymentsControl.ComboBoxBindData();
            }
        }

        /// <summary>
        /// 清空Form
        /// </summary>
        private void ClearForm()
        {
            this.id = 0;
            textbox_name.Text = String.Empty;
            radioButton_out.Checked = true;
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.SavePaymentsSort();
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_empty_Click(object sender, EventArgs e)
        {
            this.ClearForm();
        }

        #endregion

        #region 重写函数

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
            return b;
        }

        /// <summary>
        /// 单元格格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.Value != null)
            {
                if (e.ColumnIndex == 2)
                    e.Value = Int32.Parse(e.Value.ToString()) == 1 ? "收入" : "支出";
            }
        }

        #endregion

        #region 右键菜单事件

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_edit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridView1.SelectedRows[0];
                id = (long)row.Cells[0].Value;
                textbox_name.Text = row.Cells[1].Value.ToString();
                String type = row.Cells[2].Value.ToString();
                if (type.Equals(radioButton_in.Tag.ToString()))
                    radioButton_in.Checked = true;
                else
                    radioButton_out.Checked = true;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除选中分类吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        sb.Append(row.Cells[0].Value.ToString()).Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    int count = paymentsSortService.DeletePaymentsSort(sb.ToString());
                    if (count > 0)
                    {
                        MessageBox.Show("删除成功！");
                        this.DataGridViewBindingData();
                        paymentsControl.ComboBoxBindData();
                    }
                    else
                        MessageBox.Show("删除失败！");
                }
            }
        }

        #endregion
    }
}
